package com.cloud.shopping.pay.service.impl;

import com.alibaba.fastjson.JSON;
import com.cloud.shopping.pay.mapper.PayLogMapper;
import com.cloud.shopping.pay.model.PayLog;
import com.cloud.shopping.pay.service.WeChatPayService;
import com.github.wxpay.sdk.WXPay;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author John.zhang
 * @version Id: WeChatPayServiceImpl, v 1.0 2021/1/6 14:32 ZhiYong Exp $
 */
@Service
public class WeChatPayServiceImpl implements WeChatPayService {

    @Resource
    private WXPay wxPay;

    @Resource
    private PayLogMapper payLogMapper;

    @Override
    public Map<String, String> refund(Map<String, String> dataMap) throws Exception {
        return wxPay.refund(dataMap);
    }

    @Override
    public Map<String, String> preOrder(Map<String, String> dataMap) throws Exception {
        return wxPay.unifiedOrder(dataMap);
    }

    @Override
    public PayLog result(String outNo) throws Exception {
        //查询数据库中支付日志
        PayLog payLog = payLogMapper.selectById(outNo);

        if (payLog == null) {
            //数据库中没有数据查询微信支付服务
            Map<String, String> data = new HashMap<>(16);
            data.put("out_trade_no", outNo);
            Map<String, String> resp = wxPay.orderQuery(data);
            //把支付结果存入数据库中（不可逆转支付结果）
            //return_code result_code  trade_state
            String tradeState = resp.get("trade_state");
            int status = tradeState(tradeState);

            //不可逆转的支付状态，记录日志
            ArrayList<Integer> integers = Lists.newArrayList(2, 3, 4, 5, 7);
            if (integers.contains(status)) {
                payLog = new PayLog(outNo, status, JSON.toJSONString(resp), outNo, new Date());
                payLogMapper.insert(payLog);
            }
        }
        return payLog;
    }


    /***
     * 支付状态
     * @param tradeState tradeState
     * @return int
     */
    private int tradeState(String tradeState) {
        int state = 1;
        switch (tradeState) {
            //未支付
            case "NOTPAY":
                state = 1;
                break;
            //已支付
            case "SUCCESS":
                state = 2;
                break;
            //转入退款
            case "REFUND":
                state = 3;
                break;
            //已关闭
            case "CLOSED":
                state = 4;
                break;
            //已撤销
            case "REVOKED":
                state = 5;
                break;
            //用户支付中
            case "USERPAYING":
                state = 6;
                break;
            //支付失败
            case "PAYERROR":
                state = 7;
                break;
            default:
        }
        return state;
    }


}
